//
//  ZHFAttractionViewController.m
//  GoTraveling
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "ZHFAttractionViewController.h"

#import "Resources.h"
#import "ZHFTripHeadView.h"
#import "ZHFTripCell.h"
#import "ZHFAttraction.h"
#import "WSTools.h"
#import "MBProgressHUD.h"
#import "ZHFPhotoViewController.h"

@interface ZHFAttractionViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) ZHFTripHeadView *headerView;

@property (nonatomic, retain) ZHFAttraction *attraction;

@property (nonatomic, retain) MBProgressHUD *mbBarPrpgressV;

@end

@implementation ZHFAttractionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_nav_back_button"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"icon_nav_back_button"] forBarMetrics:UIBarMetricsCompactPrompt];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView.backgroundColor = self.view.backgroundColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[ZHFTripCell class] forCellReuseIdentifier:@"tripCell"];
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.mbBarPrpgressV = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.view bringSubviewToFront:self.getDataBtn];
    
    [self AFNWorking];
}

#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.attraction.tripArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *string = [[self.attraction.tripArray objectAtIndex:indexPath.row] descriptions];
    CGFloat height = [WSTools heightForContent:string size:16];
    
    if ([[self.attraction.tripArray objectAtIndex:indexPath.row] tripArray].count == 0) {
        
        return 80 + height;
    }
    else {
        return 270 + height;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZHFTripCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tripCell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    cell.contentView.backgroundColor = self.view.backgroundColor;
    NSString *string = [[self.attraction.tripArray objectAtIndex:indexPath.row] descriptions];
    CGFloat height = [WSTools heightForContent:string size:16];
    
    CGRect frame = cell.frame;
    if ([[self.attraction.tripArray objectAtIndex:indexPath.row] tripArray].count == 0) {
        frame.size.height = 80 + height;
    }
    else {
        frame.size.height = 270 + height;
    }
    cell.frame = frame;
    
    cell.trip = [self.attraction.tripArray objectAtIndex:indexPath.row];
    
    return cell;
}

#pragma mark tableView下拉放大
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat yOffset = self.tableView.contentOffset.y;
    CGFloat height = 250;
    //向上偏移量变正  向下偏移量变负
    if (yOffset < 0) {
        self.headerView.layer.masksToBounds = NO;
        CGFloat factor = ABS(yOffset) + height;
        CGRect f = CGRectMake(-([[UIScreen mainScreen] bounds].size.width*factor / height - [[UIScreen mainScreen] bounds].size.width) / 2, -ABS(yOffset), [[UIScreen mainScreen] bounds].size.width * factor / height, factor);
        self.headerView.imageView.frame = f;
    }else {
        CGRect f = _headerView.frame;
        f.origin.y = 0;
        self.headerView.layer.masksToBounds = YES;
        self.headerView.imageView.frame = CGRectMake(0, f.origin.y + yOffset / 2, [[UIScreen mainScreen] bounds].size.width, height);
        
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    if (scrollView.contentOffset.y < -100) {
        ZHFPhotoViewController *photoVC = [[ZHFPhotoViewController alloc] init];
        photoVC.Id = self.Id;
        UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:photoVC];
        navC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:navC animated:YES completion:nil];
       
    }
}

#pragma mark - 返回
- (void)leftAction:(UIBarButtonItem *)bar {
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - AFN网络请求
- (void)AFNWorking {
    
    self.getDataBtn.hidden = YES;
    [self.mbBarPrpgressV show:YES];
    NSString *strUrl = [NSString stringWithFormat:@"http://chanyouji.com/api/attractions/%@.json?page=1", self.Id];
    
    [Resources getDataWithName:strUrl block:^(id result) {
        
        NSDictionary *bigDic = result;
        self.attraction = [[ZHFAttraction alloc] init];
        [self.attraction setValuesForKeysWithDictionary:bigDic];
        
        [self createHeadView];
        
        self.headerView.attraction = self.attraction;
        [self.mbBarPrpgressV hide:YES];
        [self.tableView reloadData];
    } error:^{
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络连接错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            self.getDataBtn.hidden = NO;
            [self.mbBarPrpgressV hide:YES];
        }];
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
    }];
}

#pragma mark 点击刷新
- (void)tapAction:(UITapGestureRecognizer *)button {
    
    [self AFNWorking];
}

- (void)createHeadView {
    
    CGFloat height = [WSTools heightForContent:self.attraction.descriptions size:17];
    
    self.headerView = [[ZHFTripHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 370 + height)];
    self.headerView.backgroundColor = self.view.backgroundColor;
    self.tableView.tableHeaderView = self.headerView;    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
